package com.whh.tq;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.whh.tq.common.AndroidHelper;
import com.whh.tq.common.AsyncLoader;
import com.whh.tq.common.AsyncLoader.Callback;
import com.whh.tq.common.AsyncLoader.Execute;
import com.whh.tq.common.HttpHelper;
import com.whh.tq.common.ReqParamHelper;
import com.whh.tq.common.ResContentHelper;

public class LeftTicketInputActivity extends Activity {

	private Resources resources;
	
	private EditText editDate;
	private Dialog dateDialog;
	private EditText editFromStation;
	private EditText editToStation;
	private EditText editRandCode;
	private EditText editTrainCode;
	private Button btnRandCode;
	private Button btnQuery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leftticket_input);
		init();
	}

	private void init() {
		resources = getApplicationContext().getResources();
		editDate = (EditText) findViewById(R.id.edit_date);
		editFromStation = (EditText) findViewById(R.id.edit_from_station);
		editToStation = (EditText) findViewById(R.id.edit_to_station);
		editRandCode = (EditText) findViewById(R.id.edit_randCode);
		editTrainCode = (EditText)findViewById(R.id.edit_trainCode);
		btnRandCode = (Button) findViewById(R.id.btn_randCode);
		btnQuery = (Button) findViewById(R.id.btn_query);

		editDate.setClickable(true);
		editDate.setOnClickListener(editDateListener);
		btnRandCode.setOnClickListener(btnRandCodeListener);
		btnQuery.setOnClickListener(btnQueryListener);

		//设置默认日期
		Calendar calendar = Calendar.getInstance();
		Date time=calendar.getTime(); 
		editDate.setText(AndroidHelper.DATE_FORMAT.format(time));
		
		//获取验证图片
		loadRandCode();
		
		//获取曾经输入的数据
		SharedPreferences preferences = getSharedPreferences(AndroidHelper.PREFERENCES_NAME, Activity.MODE_PRIVATE);
		editFromStation.setText(preferences.getString("leftTicket_fromStation", ""));
		editToStation.setText(preferences.getString("leftTicket_toStation", ""));
	}
	
	/**
	 * 点击选择时间
	 */
	private View.OnClickListener editDateListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			Calendar calendar = Calendar.getInstance();
			DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker datePicker, int year,int month, int dayOfMonth) {
					String monthStr = (month+1)>9 ? ""+(month+1): "" +0+(month+1);
					String dayStr = dayOfMonth>9 ? ""+dayOfMonth: "" +0+dayOfMonth;
					editDate.setText(year + "-" + monthStr + "-"+ dayStr);
					dateDialog.dismiss();
				}
			};
			if (null == dateDialog) {
				dateDialog = new DatePickerDialog(LeftTicketInputActivity.this,
						dateListener, calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH),
						calendar.get(Calendar.DAY_OF_MONTH));
			}
			dateDialog.show();
		}
	};

	/**
	 * 点击刷新验证码
	 */
	private View.OnClickListener btnRandCodeListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			loadRandCode();
		}
	};
	
	/**
	 * 异步加载验证码
	 */
	private void loadRandCode() {
		AsyncLoader.execute(new Execute() {
			@Override
			public Object execute() {
				Drawable d = HttpHelper.getDrawable(ReqParamHelper.URL_RANDCODE);
				return d;
			}
		}, new Callback() {
			@Override
			public void callback(Object obj) {
				if (null != obj) {
					btnRandCode.setBackgroundDrawable((Drawable) obj);
				}else{
					AndroidHelper.showToast(LeftTicketInputActivity.this, resources.getString(R.string.text_get_randCode_error));
				}
			}
		});
	}	

	/**
	 * 点击查询
	 */
	private View.OnClickListener btnQueryListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			String fromStation = editFromStation.getText().toString();
			String toStation = editToStation.getText().toString();
			String date = editDate.getText().toString();
			String randCode = editRandCode.getText().toString();
			String trainCode = editTrainCode.getText().toString();
			//检查是否输入数据
			if(fromStation.equals("") || toStation.equals("")){
				AndroidHelper.showToast(getApplicationContext(), resources.getString(R.string.text_from_where_to_where));
				return ;
			}
			
			if(randCode.length()<1){
				AndroidHelper.showToast(getApplicationContext(),resources.getString(R.string.text_input_randCode));
				return ;				
			}
			
			//检查是否中文的地点
			if(!(ResContentHelper.isChineseStr(fromStation) && ResContentHelper.isChineseStr(toStation))){
				AndroidHelper.showToast(getApplicationContext(), resources.getString(R.string.text_no_english_city));
				return ;
			}
			
		
			SharedPreferences preferences = getSharedPreferences(AndroidHelper.PREFERENCES_NAME, Activity.MODE_PRIVATE);
			Editor editor = preferences.edit();
			editor.putString("leftTicket_fromStation", fromStation);
			editor.putString("leftTicket_toStation", toStation);
			editor.commit();
			Intent intent = new Intent();
			intent.setClass(LeftTicketInputActivity.this,LeftTicketResultActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("fromStation", fromStation);
			bundle.putString("toStation", toStation);
			bundle.putString("date", date);
			bundle.putString("randCode",randCode);
			bundle.putString("trainCode", trainCode);
			intent.putExtras(bundle);
			startActivity(intent);
			//先重置验证码为获取中...，再异步加载
			//Resources resources = getApplicationContext().getResources();   
			//Drawable gettingRandcode = resources.getDrawable(R.drawable.getting_randcode);  
			//btnRandcode.setBackgroundDrawable(gettingRandcode);
			//loadRandCode();
		}
	};

}
